<template>
  <div style="display: flex; flex-direction: row; height: auto">
    <div :style="{ width: leftPanelWidth + 'px' }">
      <el-scrollbar height="88vh">
        <el-table
          ref="tableTreeRef"
          :data="bomInfos"
          style="width: 100%; margin-bottom: 20px"
          max-height="650px"
          row-key="child_item_revision_uid"
          :expand-row-keys="expand_row_keys"
          border
          lazy
          fit
          table-layout="auto"
          :load="loadChildrenBomInfo"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          v-loading="isDownloading"
          element-loading-text="文件生成中..."
        >
          <el-table-column
            prop="child_erp_id"
            label="编码"
            sortable
            width="200px"
          />
          <el-table-column
            prop="child_name"
            label="名称"
            sortable
            width="250px"
          >
            <template #default="{ row }">
              <div class="bom-table-first-col">
                <img :src="getIcon(row)" width="20px" height="20px" />
                {{ row.child_name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="qty_value" label="数量" sortable />
          <el-table-column prop="procurementType" label="采购类型" sortable />
          <el-table-column
            prop="priceInfo.KEPH_SUM"
            label="总成本"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="priceInfo.KEPH_SUM"
            label="单位成本"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="priceInfo.KEPH_KST001"
            label="材料成本"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="priceInfo.KEPH_MANUFACTURE"
            label="制造成本"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="priceInfo.KEPH_OTHER"
            label="其他成本"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="elapsedTime"
            label="工时"
            :formatter="formatter"
            sortable
          />
          <el-table-column
            prop="priceInfo.vendor_name"
            label="供应商"
            :formatter="formatter"
            sortable
          />
        </el-table>
      </el-scrollbar>
    </div>
    <div class="resizer" @mousedown="startResize"></div>
    <div></div>
  </div>
</template>

<script lang="ts" setup>
import { Search } from "@element-plus/icons-vue";
import { onMounted } from "vue";
import { useRouter } from "vue-router";
import { ref, getCurrentInstance, nextTick, Ref, reactive } from "vue";
import { getIconByType } from "../vendor/TypeRendering.js";

const instance = getCurrentInstance();
const { $http } = instance?.appContext.config.globalProperties;

const leftPanelWidth = ref(300); // 初始左边面板宽度
const expand_row_keys = ref([]);
const isDownloading = ref(false);
const bomInfos = ref([]);

const formatter = (row: any, column: any, cellValue: any) => {
  if (typeof cellValue === "number") {
    return cellValue.toFixed(2); // 保留小数点后两位
  } else {
    return cellValue; // 其他类型的数据原样返回
  }
};

const getIcon = (item: any) => {
  console.log(item);
  console.log(item.child_type);
  return getIconByType(item.child_type);
};

const loadChildrenBomInfo = (bomInfo: any, treeNode: any, resolve: any) => {
  console.log("loadChildrenBomInfo", bomInfo, treeNode);
  console.log("child uid = ", bomInfo.child_item_revision_uid);
  const url: string =
    "/getBomInfo2?parentUid=" + bomInfo.child_item_revision_uid;
  console.log(url);
  $http
    .get(url)
    .then((response: any) => {
      console.log(response.data);
      resolve(response.data);
    })
    .catch((error: any) => {
      console.log(error);
    });
};

const _ = window.ResizeObserver;

window.ResizeObserver = class ResizeObserver extends _ {
  constructor(callback: any) {
    callback = debounce(callback, 20);
    super(callback);
  }
};
const debounce = (
  callback: { apply: (arg0: any, arg1: any[]) => void },
  delay: number | undefined
) => {
  let tid: number | undefined;
  return function (...args: any) {
    tid && clearTimeout(tid);
    tid = setTimeout(() => {
      callback.apply(getCurrentInstance(), args);
    }, delay);
  };
};

const startResize = () => {
  document.addEventListener("mousemove", resize);
  document.addEventListener("mouseup", stopResize);
};

const resize = (event: MouseEvent) => {
  leftPanelWidth.value = event.clientX;
};

const stopResize = () => {
  document.removeEventListener("mousemove", resize);
  document.removeEventListener("mouseup", stopResize);
};
</script>
: any: any: number
